import defaultImgUrl from'@/assets/img/image-default.png';

/**
 * 图片地址格式化
 * @param url 源链接
 * @param size 尺寸
 * @returns 
 */
const formatImgUrl = (url: string, size?: string): string => {
  if (!url || url.trim() == '') return defaultImgUrl;
    const isUnWatermark = /images(-beta)?\.yj(yz|zf)\.com/.test(url) // true 不带水印
    const newUrl = url.indexOf('?') > -1 ? url.split('?')[0] : url
    if (size) {
        //  带尺寸图片
        const [w, h] = size.split('x')
        return isUnWatermark
            ? newUrl + `?x-oss-process=image/auto-orient,1/resize,m_fill,w_${w},h_${h}/quality,q_90`
            : newUrl + '?x-oss-process=style/centerlogo' + size
    } else {
        return isUnWatermark ? newUrl : newUrl + '?x-oss-process=style/centerlogo'
    }
}

/**
 * 毫秒转 时分
 * @param time 
 * @returns 
 */
const toSeconds = (time: number): string => {
  time = time || 0
  time = parseInt((time / 1000) + '', 10)
  if (time > 60) {
      let minute = Math.floor(time / 60)
      let second = time - minute * 60
      return minute + "'" + second + '"'
  } else {
      return time + '"'
  }
}

/**
 * 设置localStorage
 * @param key 
 * @param value 
 */
const setStorage = (key: string, value: object | string) => {
  let v: string = typeof value === 'object' ? JSON.stringify(value) : value
  localStorage.setItem(key, v)
}

/**
 * 获取 localStorage
 * @param key 
 * @returns
 */
const getStorage = (key: string): any =>  {
  let v = localStorage.getItem(key)
  if (v) {
    try {
        return JSON.parse(v)
    } catch (e) {
        return v
    }
  }
  return v
}

/**
 * 
 * @param timestamp 时间戳
 * @param farmat 格式类型
 * @returns 格式时间
 */
const formatDate = (timestamp: number | string, farmat?: string) => {
  const trans10 = (num: number): number | string => {
      return num >= 10 ? num : '0' + num
  }
  if (!timestamp) return ''
  let date = new Date(+timestamp)
  const y = date.getFullYear(),
      m = trans10(date.getMonth() + 1),
      d = trans10(date.getDate()),
      h = trans10(date.getHours()),
      mm = trans10(date.getMinutes()),
      ss = trans10(date.getSeconds())
  if (farmat == 'ymdhm') {
      return y + '/' + m + '/' + d + ' ' + h + ':' + mm
  }
  if (farmat == 'ymd') {
      return y + '/' + m + '/' + d
  }
  return y + '/' + m + '/' + d + ' ' + h + ':' + mm + ':' + ss
}

interface examTagsType {
  fullPath: string;
  [propName: string]: string | object;
}

/**
 * 获取当前路由之前的路由信息
 * @param path 
 * @returns { fullPath, examTags }
 */
const getPrevRouteInfo = (path: string): {
  fullPath: string;
  examTags: examTagsType[];
} => {
  const examTags = localStorage.getItem('examTags');
  if (examTags) {
    const _examTags: examTagsType[] = JSON.parse(examTags);
    const currentIndex = _examTags.findIndex((item) => item.fullPath === path);
    let fullPath = '';
    if (currentIndex - 1 < 0 ) {
      fullPath = '/'
    } else {
      fullPath = _examTags[currentIndex - 1].fullPath;
    }
    _examTags.splice(currentIndex, 1)
    return {
      fullPath,
      examTags: _examTags
    }
  } else {
    return {
      fullPath: '/',
      examTags: []
    }
  }
}

export {
  formatImgUrl,
  toSeconds,
  setStorage,
  getStorage,
  formatDate,
  getPrevRouteInfo
}
